import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# ====== 设置中文字体 ======
plt.rcParams['font.sans-serif'] = ['SimHei']   # 黑体
plt.rcParams['axes.unicode_minus'] = False     # 解决负号显示问题

# ====== 构造数据：专家维度 ======
data_expert = {
    "专家维度": [
        "城市更新与系统优化",
        "空间利用与人居环境",
        "品质提升与设施改善",
        "战略规划与发展思路",
        "建设导向与空间塑造",
        "产业经济与结构优化",
        "宜居环境与生活品质",
    ],
    "人工数据偏好分布占比(%)": [14.75, 14.75, 14.75, 14.75, 14.53, 14.10, 12.38],
    "ppo专家模型": [17.09, 17.09, 17.09, 15.38, 12.82, 11.11, 9.40],
    "ppo融合模型": [16.53, 16.53, 16.53, 16.53, 13.22, 13.22, 7.44],
    "ppo公众模型": [15.15, 15.15, 15.15, 15.15, 13.64, 12.88, 12.88],
    "基础模型": [15.04, 15.04, 15.04, 15.04, 14.29, 13.53, 12.03],
    "dpo专家模型": [15.04, 15.04, 15.04, 15.04, 13.53, 13.53, 12.78],
    "dpo公众模型": [14.81, 14.81, 14.81, 14.81, 14.07, 14.07, 12.59],
    "dpo融合模型": [14.60, 14.60, 14.60, 14.60, 14.60, 13.87, 13.14]
}
df_expert = pd.DataFrame(data_expert)

# ====== 构造数据：公众维度 ======
data_public = {
    "公众维度": [
        "整体发展与宜居生活",
        "空间利用与生态环境",
        "配套设施改善",
        "土地与资源效率",
        "产业经济与创新发展",
        "公共服务与社区生活",
        "文化传承与历史保护",
    ],
    "人工数据偏好分布占比(%)": [15.9, 15.9, 15.82, 15.82, 15.43, 14.43, 6.71],
    "ppo专家模型": [16.81, 16.81, 15.13, 15.13, 15.13, 12.61, 8.40],
    "ppo融合模型": [16.67, 16.67, 16.67, 15.00, 14.17, 13.33, 7.50],
    "ppo公众模型": [15.04, 15.04, 15.04, 15.04, 14.29, 12.78, 12.78],
    "基础模型": [16.00, 16.00, 16.00, 15.20, 15.20, 15.20, 6.40],
    "dpo专家模型": [15.87, 15.87, 15.87, 15.08, 15.08, 14.29, 7.94],
    "dpo公众模型": [15.87, 15.87, 15.87, 15.08, 15.08, 15.08, 7.14],
    "dpo融合模型": [15.38, 15.38, 15.38, 15.38, 15.38, 15.38, 7.69],
}
df_public = pd.DataFrame(data_public)

# ====== 绘制差值热力图函数 ======
def plot_diff_heatmap(df, title, dim_col):
    human = df["人工数据偏好分布占比(%)"].values
    model_cols = [
        "ppo专家模型", "ppo融合模型",
        "ppo公众模型", "基础模型","dpo专家模型", "dpo公众模型", "dpo融合模型"
    ]
    diff = df[model_cols].sub(human, axis=0)
    diff.insert(0, "人工数据", 0)
    diff.index = df[dim_col]

    plt.figure(figsize=(12, 8))  # 调大画布
    sns.heatmap(
        diff, annot=True, cmap="RdYlGn", center=0, fmt=".2f",
        annot_kws={"size": 12}
    )
    plt.title(title, fontsize=18)
    plt.xlabel("模型", fontsize=14)
    plt.ylabel("维度", fontsize=14)
    plt.xticks(fontsize=12, rotation=30)
    plt.yticks(fontsize=12, rotation=0)  # 横向显示
    plt.tight_layout()  # 自动调整边距
    plt.show()

# ====== 绘制专家维度热力图 ======
plot_diff_heatmap(df_expert, "专家维度：模型与人工数据的差值热力图", "专家维度")

# ====== 绘制公众维度热力图 ======
plot_diff_heatmap(df_public, "公众维度：模型与人工数据的差值热力图", "公众维度")
